<?php
/**
 * demo数据仓库
 *
 * @author: yanxs <541139655@qq.com>
 * @version: 1.0
 * @date: 2021/3/15
 */

namespace App\Repositories;

use App\Models\Demo;
use App\Presenters\CommonPresenter;

class DemoRepository extends BaseRepository
{
    public function __construct(Demo $model)
    {
        $this->model =$model;
    }

    /**
     * 将数据库的状态、标识、类型转化为实际值
     * 支持一维、二维数组
     * @param $data
     * @return mixed|void
     */
    public function transFlagToEntity($data)
    {
        if (!empty($data)) {
            $commonPresenter = app(CommonPresenter::class);
            if (isset($data[0]) && is_array($data[0])) { //列表list处理
                foreach ($data as $k=>$v) {
                    $data[$k]['status'] = $commonPresenter->getEnabledOrDisabled($v['status']); //转化状态
                    $data[$k]['check_status'] = $commonPresenter->getCheckStatus($v['check_status']); //转化状态
                    $data[$k]['created_at'] = $commonPresenter->exchangeTime($v['created_at']); //转化状态
                    $data[$k]['updated_at'] = $commonPresenter->exchangeTime($v['updated_at']); //转化状态
                }
            } else { //单条信息处理
                $data['status'] = $commonPresenter->getEnabledOrDisabled($data['status']); //转化状态
                $data['check_status'] = $commonPresenter->getEnabledOrDisabled($data['check_status']); //转化状态
                $data['created_at'] = $commonPresenter->exchangeTime($data['created_at']); //转化状态
                $data['updated_at'] = $commonPresenter->exchangeTime($data['updated_at']); //转化状态
            }
        }

        return $data;
    }

    /**
     * 实体转化为标识
     * @param $data
     * @return mixed
     */
    public function unTransFlagToEntity($data)
    {
        if (!empty($data)) {
            if (isset($data[0]) && is_array($data[0])) { //列表list处理
                foreach ($data as $k=>$v) {
                    $data[$k]['status'] = 1; //转化状态
                    $data[$k]['check_status'] = 1; //转化状态
                }
            } else { //单条信息处理
                $data['status'] = 1; //转化状态
                $data['check_status'] = 1; //转化状态
            }
        }

        return $data;
    }
}
